{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "273d7bdc-8509-4525-b7a7-2acb1e2028e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.schema import HumanMessage\n",
    "from langchain.prompts import FewShotChatMessagePromptTemplate, ChatPromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ca1b03c6-991c-4911-b75b-416d3806c5ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "example_prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"human\", \"格式化以下客户信息：\\n姓名 -> {customer_name}\\n年龄 -> {customer_age}\\n 城市 -> {customer_city}\"),\n",
    "        (\"ai\", \"##客户信息\\n- 客户姓名：{formatted_name}\\n- 客户年龄：{formatted_age}\\n- 客户所在地：{formatted_city}\")\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f0ea60fe-1d39-4422-9551-aec04745d620",
   "metadata": {},
   "outputs": [],
   "source": [
    "examples = [\n",
    "    {\n",
    "        \"customer_name\": \"张三\", \n",
    "        \"customer_age\": \"27\",\n",
    "        \"customer_city\": \"长沙\",\n",
    "        \"formatted_name\": \"张三\",\n",
    "        \"formatted_age\": \"27岁\",\n",
    "        \"formatted_city\": \"湖南省长沙市\"\n",
    "    },\n",
    "    {\n",
    "        \"customer_name\": \"李四\", \n",
    "        \"customer_age\": \"42\",\n",
    "        \"customer_city\": \"广州\",\n",
    "        \"formatted_name\": \"李四\",\n",
    "        \"formatted_age\": \"42岁\",\n",
    "        \"formatted_city\": \"广东省广州市\"\n",
    "    },\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "223e9d82-2c78-4d5f-9cb0-04af2b0e1d8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "few_shot_template = FewShotChatMessagePromptTemplate(\n",
    "    example_prompt=example_prompt,\n",
    "    examples=examples,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ad6cfabe-a7a0-47ed-bcc2-7a80641c8f7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_prompt_template = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        few_shot_template,\n",
    "        (\"human\", \"{input}\"),\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a4812cc0-5d2f-49df-91a8-f61a79a828c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_prompt = final_prompt_template.invoke({\"input\": \"格式化以下客户信息：\\n姓名 -> 王五\\n年龄 -> 31\\n 城市 -> 郑州'\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "676d7f6e-c18a-424a-b6a8-87180ea086d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[HumanMessage(content='格式化以下客户信息：\\n姓名 -> 张三\\n年龄 -> 27\\n 城市 -> 长沙'),\n",
       " AIMessage(content='##客户信息\\n- 客户姓名：张三\\n- 客户年龄：27岁\\n- 客户所在地：湖南省长沙市'),\n",
       " HumanMessage(content='格式化以下客户信息：\\n姓名 -> 李四\\n年龄 -> 42\\n 城市 -> 广州'),\n",
       " AIMessage(content='##客户信息\\n- 客户姓名：李四\\n- 客户年龄：42岁\\n- 客户所在地：广东省广州市'),\n",
       " HumanMessage(content=\"格式化以下客户信息：\\n姓名 -> 王五\\n年龄 -> 31\\n 城市 -> 郑州'\")]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_prompt.messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e18ae042-004a-4019-9beb-f9d0604ab4aa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "##客户信息\n",
      "- 客户姓名：王五\n",
      "- 客户年龄：31岁\n",
      "- 客户所在地：河南省郑州市\n"
     ]
    }
   ],
   "source": [
    "model = ChatOpenAI(model=\"gpt-3.5-turbo\")\n",
    "response = model.invoke(final_prompt)\n",
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c392ddc2-e4b3-4bc5-bf40-2ab52719bc45",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
